﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LGSVR.Common;
using LGSVR.BLL.Terminal;
using System.Data;

namespace LGSVR_WEB.terminal
{
    public partial class CardLightEdit : DataBasePage
    {
        private CardLightBLL cardLight = new CardLightBLL();
        private string GroupID
        {
            get {
                return Request["groupID"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                Dictionary<int, int> lens = new Dictionary<int, int>();
                for (int i = 0; i < 11; i++)
                {
                    lens.Add(i, i);
                }
                dropActCount.DataSource = lens;
                dropActCount.DataTextField = "key";
                dropActCount.DataValueField = "value";
                dropActCount.DataBind();

                BindInfo();
            }
        }

        private void BindInfo()
        {
            DataTable dt = cardLight.GetCardLightByGroupID(GroupID);
            rept.DataSource = dt;
            rept.DataBind();
            dropActCount.SelectedValue = dt.Rows.Count.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dicParam = new Dictionary<string, string>();
                //删除if
                Result = cardLight.DeleteByGroupID(GroupID);

                for (var i = 1; i < int.Parse(dropActCount.SelectedValue) + 1; i++)
                {
                    if (Request["txtTimeBegin" + i].Trim() != "" && Request["txtLightType" + i].Trim() != "")
                    {
                        dicParam = new Dictionary<string, string>();
                        dicParam.Add("timeBegin", Request["txtTimeBegin" + i].Trim());
                        dicParam.Add("lightType", Request["txtLightType" + i].Trim());
                        dicParam.Add("content", Request["txtContent" + i].Trim());
                        dicParam.Add("cardGroup", GroupID);
                        Insert("cardLight", dicParam);
                    }
                   
                }
                if (Result > 0)
                {
                    MsgContent = ShowMessage("恭喜您，保存成功！", MessageType.Success, false, true);
                    NoRefresh();
                }
                else
                {
                    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                }
            }
            catch {
                MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
            }
            
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["backUrlParam"]))
            {
                Response.Redirect("CardGroupList.aspx?" + Request.QueryString["backUrlParam"], false);
            }
            else
            {
                Response.Redirect("CardGroupList.aspx", false);
            }
        }
    }
}